<template>
	<view>
		<view class="live">
			<view v-if="!isplay" class="overlay" @click="playVideo">
				<image src="/static/play.png" mode="widthFix"></image>
			</view>
			<view class="live_box" v-if="liveInfo">
				<yingbing-video :muted='isMuted' :src="liveInfo.liveUrl" @play='autoPlay' :controls='false'
					ref="videoItem" />
			</view>
			<view class="live_info" @click.stop="isShowInput = false;isShowShopBag=false">
				<view class="head">
					<view class="actor_info" v-if="liveInfo">
						<view class="actor-avatar" :style="{backgroundImage:`url(${liveInfo.actorInfo.avatar})`}">

						</view>
						<view class="info">
							<view class="actor_name">
								{{liveInfo.actorInfo.uname}}
							</view>
							<view class="online_num">
								热度 {{liveInfo.onlineUserCount}}
							</view>
						</view>
						<view class="actor-fans">
							<view class="btn">
								关注
							</view>
						</view>
					</view>
				</view>
				<view class="recommend-goods">
					<view class="goods-item" v-if="sellPro" :style="{backgroundImage:`url(${sellPro[0].image})`}">

						<view class="spike-icon-doing">
							秒杀中
						</view>
						<view class="price">
							￥{{(sellPro[0].defaultPrice/100).toFixed(2)}}
						</view>
					</view>
					<view class="goods-item" style="margin-bottom: 0;" v-if="sellPro"
						:style="{backgroundImage:`url(${sellPro[1].image})`}">

						<view class="spike-icon-doing">
							秒杀中
						</view>
						<view class="price">
							￥{{(sellPro[1].defaultPrice/100).toFixed(2)}}
						</view>
					</view>
				</view>



				<view class="footer_mask">
					<view class="comments">
						<view class="comments-list">
							<view class="official-tip" v-if="liveInfo">
								{{liveInfo.noticeContent}}
							</view>
							<view class="comment-List" v-if="commentList.length>0" v-for="(item,index) in commentList">
								<view class="name">
									{{item.username}}：
								</view>
								<view class="intro">
									{{item.action}}
								</view>
							</view>
						</view>
					</view>
					<view class="footer-section">
						<view class="goods_shop">
							<view class="icon-sell-bag" @click.stop="isShowShopBag=true">
								<view class="sell-bag-num" v-if="sellPro">
									{{sellPro.length}}
								</view>
							</view>
							<view class="comment-box" @click.stop="showInput">
								说点什么...
							</view>
						</view>

						<view class="fans-click">
							<view class="icon-Share">

							</view>
							<view class="icon-comment">

							</view>
						</view>
					</view>



				</view>
			</view>
			<view class="comment-input" v-if="isShowInput">
				<input type="text" v-model="inp_value" />
				<view class="input-btn" @click="sendInp">
					发送
				</view>
			</view>

			<view class="sell_bag" v-if="isShowShopBag">

				<view class="goods-count">
					全部商品{{sellPro.length}}
				</view>
				<view class="goods-item-list">
					<view class="goods-item" v-for="(item,index) in sellPro">
						<view class="goods-img" :style="{backgroundImage:`url(${item.image})`}">
							<view class="goods-index">
								{{index+1}}
							</view>
						</view>
						<view class="goods-msg-wrap">
							<view class="goods-msg">
								<view class="goods-tag">
									秒杀中
								</view>
								<view class="goods-title">
									{{item.title}}
								</view>
							</view>
							<view class="goos-btn-wrap">
								<view class="goods-price-wrap">
									<view class="old-price" v-if="!item.highPrice">
										￥{{(item.originalPrice/100).toFixed(2)}}
									</view>
									<view class="now-price">
										{{item.highPrice ? '￥'+(item.defaultPrice/100).toFixed(2)+'~￥'+(item.highPrice/100).toFixed(2):'￥'+(item.defaultPrice/100).toFixed(2)}}
									</view>
								</view>
								<view class="to-buy-btn">
									去购买
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>

		</view>

	</view>
</template>

<script setup>
	import yingbingVideo from '../../uni_modules/yingbing-video/components/yingbing-video/yingbing-video.vue'
	import {
		ref,
		onMounted,
		nextTick
	} from 'vue'
	import {
		onLoad
	} from '@dcloudio/uni-app'
	import {
		get
	} from '../../api'
	const isShowInput = ref(false)
	const liveInfo = ref(null)
	const videoItem = ref(null)
	const isMuted = ref(true);
	const isplay = ref(false)
	const isShowShopBag = ref(false)

	const commentList = ref([])
	const sellPro = ref([])

	const inp_value = ref('')
	const count = ref(0)


	const fetchLiveData = async (roomId, actorId) => {
		try {
			const res = await get('/live/actorLiveInfo', {
				roomId,
				actorId
			})
			console.log(res.data);

			if (!res?.data?.liveUrl) {
				throw new Error('无效的直播URL')
			}

			liveInfo.value = res.data
			console.log('直播URL:', liveInfo.value.liveUrl)
		} catch (err) {
			console.error('获取直播失败:', err)
			liveInfo.value = null
		}
	}

	const getLiveRoomPro = async (roomId, actorId) => {
		try {
			const res = await get('/live/queryDiscountPrice', {
				roomId,
				actorId
			})
			sellPro.value = res.data.itemList
		} catch (error) {
			//TODO handle the exception
			console.error('获取促销商品失败:', error)
		}
	}

	const getLiveComments = async () => {
		try {
			const res = await get('/live/comments')
			console.log('直播弹幕', res);
			const arr = res
			if (arr) {
				setInterval(() => {
					commentList.value = [...commentList.value, ...arr.splice(0, 1)]
				}, 2000)
			}
		} catch (error) {
			//TODO handle the exception
		}
	}
	const autoPlay = () => {
		isMuted.value = false
	}
	const playVideo = () => {
		if (liveInfo.value.liveUrl) {
			isplay.value = true
			videoItem.value.play()
		}
	}
	const showInput = () => {
		isShowInput.value = !isShowInput.value
		if (isShowInput.value) {
			nextTick(() => {

			})
		}
	}

	const sendInp = () => {
		if (!inp_value.value.trim()) return
		count.value++
		const obj = {}
		obj.username = '用户' + count.value
		obj.action = inp_value.value.trim()
		commentList.value = [...commentList.value, obj]
		inp_value.value = ''
		isShowInput.value = false
	}

	onLoad((options) => {
		fetchLiveData(options.roomId, options.actorId)
		getLiveRoomPro(options.roomId, options.actorId)
		getLiveComments()
	})
</script>

<style lang="scss">
	* {
		box-sizing: border-box;
	}

	.live {
		width: 100vw;
		height: 100vh;
		position: relative;
		overflow: hidden;
		background-color: #000;

		.live_box {
			height: 100vh;
			width: 100vw;
			overflow: hidden;
		}

		.comment-input {
			position: absolute;
			bottom: 0;
			width: 100%;
			height: 100rpx;
			background-color: #333;
			padding: 20rpx;
			z-index: 400;
			display: flex;
			align-items: center;

			input {
				flex: 1;
				margin-right: 48rpx;
				padding-left: 20rpx;
				width: 436rpx;
				height: 60rpx;
				border-radius: 8rpx;
				background-color: rgba(255, 87, 119, .8);
				border: none;
				color: #ffffff;
				font-size: 28rpx;
			}

			.input-btn {
				width: 120rpx;
				height: 60rpx;
				border-radius: 4rpx;
				background-color: #ff5777;
				color: #fff;
				font-size: 28rpx;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}

		.sell_bag {
			display: flex;
			flex-direction: column;
			flex: 1;
			overflow-y: auto;
			position: fixed;
			bottom: 0;
			left: 20rpx;
			width: 710rpx;
			height: 864rpx;
			z-index: 1000;
			border-radius: 8rpx 8rpx 0 0;
			background: rgba(0, 0, 0, .55);
			backdrop-filter: blur(15rpx);

			.goods-count {
				padding-left: 24rpx;
				width: 100%;
				height: 80rpx;
				font-size: 28rpx;
				color: #fff;
				flex-grow: 0;
				flex-shrink: 0;
				line-height: 80rpx;
				background-color: #737373;
			}

			.goods-item-list {
				height: 100%;
				overflow: scroll;

				.goods-item {
					padding: 24rpx;
					width: 710rpx;
					height: 248rpx;
					background-color: rgba(0, 0, 0, .5);
					display: flex;
					column-gap: 18rpx;

					.goods-img {
						position: relative;
						width: 200rpx;
						height: 200rpx;
						background-repeat: no-repeat;
						background-size: cover;
						border-radius: 6rpx;

						.goods-index {
							display: flex;
							align-items: center;
							justify-content: center;
							position: absolute;
							top: 0;
							left: 0;
							height: 40rpx;
							width: 45rpx;
							line-height: 28rpx;
							font-size: 28rpx;
							color: #fff;
							background-color: #413a3a;
							border-radius: 0 0 21rpx 0;
							text-align: center;
						}
					}

					.goods-msg-wrap {
						width: 438rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;

						.goods-msg {
							display: flex;
							flex-direction: column;
							row-gap: 8rpx;

							.goods-tag {
								background-image: linear-gradient(136deg, #fb872c, #fc4242);
								width: 76rpx;
								height: 32rpx;
								line-height: 32rpx;
								font-size: 20rpx;
								color: #fff;
								flex-shrink: 0;
								text-align: center;
							}

							.goods-title {
								width: 438rpx;
								height: 40rpx;
								line-height: 40rpx;
								font-size: 28rpx;
								color: #fff;
								overflow: hidden;
								text-overflow: ellipsis;
								white-space: nowrap;
							}
						}

						.goos-btn-wrap {
							display: flex;
							align-items: center;
							justify-content: space-between;

							.goods-price-wrap {
								width: 259rpx;
								height: 56rpx;
								position: relative;
								display: flex;
								flex-direction: column;
								justify-content: center;

								.old-price {
									width: fit-content;
									height: 28rpx;
									font-size: 20rpx;
									color: #999;
									text-decoration: line-through;
								}

								.now-price {
									height: 40rpx;
									font-size: 28rpx;
									color: #f46;
									width: fit-content;
								}
							}

							.to-buy-btn {
								margin-top: 5rpx;
								width: 120rpx;
								height: 50rpx;
								line-height: 38rpx;
								font-style: 26rpx;
								color: #fff;
								background-color: #f46;
								border-radius: 8rpx;
								display: flex;
								justify-content: center;
								align-items: center;
							}
						}
					}
				}
			}

		}
	}

	.overlay {
		position: absolute;
		top: 50%;
		left: 50%;
		width: 220rpx;
		height: 220rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		color: white;
		z-index: 999;
		transform: translate(-50%, -50%);

		image {
			width: 220rpx;
		}
	}

	.live_info {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		z-index: 300;
		display: flex;
		flex-direction: column;

		box-sizing: border-box;


		.head {
			padding-top: 18rpx;
			padding-left: 10rpx;
			background-image: linear-gradient(0deg, transparent, rgba(0, 0, 0, .02) 18%, rgba(0, 0, 0, .6));

			.actor_info {
				background: rgba(0, 0, 0, .1);
				border-radius: 10000rpx;
				padding: 10rpx 20rpx 10rpx 15rpx;
				display: flex;
				align-items: center;
				float: left;
				column-gap: 12rpx;

				.actor-avatar {
					margin-right: 10rpx;
					margin-top: 5rpx;
					width: 70rpx;
					height: 70rpx;
					border-radius: 50%;
					background-repeat: no-repeat;
					background-size: 100% auto;
					border: 1rpx solid #999;
				}

				.info {
					color: #fff;

					.actor_name {
						font-weight: 700;
						line-height: 1;

					}

					.online_num {
						font-size: 22rpx;
					}
				}

				.actor-fans {

					.btn {
						font-size: 24rpx;
						height: 50rpx;
						color: #fff;
						width: 88rpx;
						border-radius: 100rpx;
						background-image: linear-gradient(133deg, #ff5777, #ff469f);
						text-align: center;
						line-height: 50rpx;
					}
				}
			}
		}

		.recommend-goods {
			margin-top: 10rpx;
			margin-left: 10rpx;
			padding: 8rpx 8rpx 8rpx 9rpx;
			width: 148rpx;
			border-radius: 8rpx;
			background-color: rgba(0, 0, 0, .2);

			.goods-item {
				position: relative;
				margin-bottom: 16rpx;
				height: 120rpx;
				background-repeat: no-repeat;
				background-size: cover;
				border-radius: 10rpx;

				.spike-icon-doing {
					position: absolute;
					top: 0;
					left: 0;
					padding: 3rpx 8rpx 0;
					color: #fff;
					font-size: 18rpx;
					border-radius: 11rpx 0 10rpx 0;
					background-image: linear-gradient(90deg, #ff8942, #fb4d50);
				}

				.price {
					position: absolute;
					bottom: 0;
					padding: 0 10rpx;
					color: #fff;
					font-size: 24rpx;
					left: 0;
					background-color: rgba(0, 0, 0, .7);
					border-radius: 0 10rpx 0 10rpx;
					max-width: 100rpx;
					white-space: nowrap;
					overflow: hidden;
				}
			}
		}

		.footer_mask {
			margin-top: auto;
			padding-top: 20rpx;
			background-image: linear-gradient(180deg, transparent, rgba(0, 0, 0, .02) 18%, rgba(0, 0, 0, .4));

			.comments {
				margin-left: 20rpx;
				width: 550rpx;
				height: 400rpx;
				overflow-y: auto;
				-webkit-overflow-scrolling: touch;
				scroll-behavior: smooth;

				scrollbar-width: none;
				/* Firefox */
				-ms-overflow-style: none;
				/* IE/Edge */

				&::-webkit-scrollbar {
					display: none;
					/* Chrome/Safari/Webkit */
					width: 0 !important;
					height: 0 !important;
					background: transparent;
				}

				.comments-list {


					.official-tip {
						margin-bottom: 10rpx;
						font-size: 20rpx;
						color: #fff;
					}

					.comment-List {
						display: flex;
						padding: 10rpx 16rpx;
						width: fit-content;
						height: 50rpx;
						border-radius: 10rpx;
						background-color: rgba(0, 0, 0, .1);
						font-size: 24rpx;
						align-items: center;
						gap: 5rpx;
						margin-top: 5rpx;

						.name {
							color: #ff5777;
							white-space: nowrap;
						}

						.intro {
							color: #fff;
						}
					}
				}

			}

			.footer-section {
				margin: 20rpx 20rpx 30rpx;
				display: flex;
				-webkit-box-pack: justify;
				align-items: center;
				justify-content: space-between;

				.goods_shop {
					display: flex;
					gap: 15rpx;
					align-items: center;

					.icon-sell-bag {
						width: 90rpx;
						height: 90rpx;
						position: relative;
						background-image: url('/static/shopbig.gif');
						background-size: cover;

						.sell-bag-num {
							position: absolute;
							top: 50rpx;
							left: 50rpx;
							width: 45rpx;
							height: 45rpx;
							text-align: center;
							margin-left: -28rpx;
							margin-top: -3rpx;
							color: #fff;
							font-size: 28rpx;
						}
					}

					.comment-box {
						width: 230rpx;
						height: 70rpx;
						line-height: 52rpx;
						color: #fff;
						background: rgba(0, 0, 0, .2);
						padding: 10rpx 25rpx;
						border-radius: 100rpx;
					}
				}

				.fans-click {
					display: flex;
					gap: 20rpx;

					.icon-Share {
						width: 70rpx;
						height: 70rpx;
						background-color: rgba(0, 0, 0, .3);
						border-radius: 50%;
						background-size: 40rpx 40rpx;
						background-repeat: no-repeat;
						background-position: 50%;
						background-image: url('/static/Share.png');
					}

					.icon-comment {
						width: 70rpx;
						height: 70rpx;
						background-color: rgba(0, 0, 0, .3);
						border-radius: 50%;
						background-size: 40rpx 40rpx;
						background-repeat: no-repeat;
						background-position: 50%;
						background-image: url('/static/comment.png');
					}
				}
			}
		}


	}
</style>